﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using mBook.ViewModels;
using mBook.DataManager;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace mBook.Views
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class DownloadingPage : mBook.Common.LayoutAwarePage
    {

        public DownloadingPage()
        {
            this.InitializeComponent();
        }

        public void StopDownload(EbookViewModel ebook)
        {
            int index = ManagerDataViewModel.GetAllDownloadingEbooks().IndexOf(ebook);
            //downloading ebook -> stop download
            if (index == 0)
            {
                DataService.GetInstance().StopDownload(ebook.EBook.MediaId);
            }

            ManagerDataViewModel.GetAllDownloadingEbooks().Remove(ebook);

            UpdateInfoDownload();
        }

        void UpdateInfoDownload()
        {
            String text = "";
            if (ManagerDataViewModel.GetAllDownloadingEbooks().Count > 0)
            {

                if (ManagerDataViewModel.GetAllDownloadingEbooks().Count > 1)
                {
                    text = "Downloading " + ManagerDataViewModel.GetAllDownloadingEbooks().Count + " ebooks...";
                }
                else
                {
                    text = "Downloading 1 ebook...";
                }

            }
            else
            {
                text = "";
            }

            ManagerDataViewModel.SetStatus(text);

        }

        public void PauseDownload(EbookViewModel ebook)
        {
            DataService.GetInstance().PauseDownload(ebook.EBook.MediaId);
        }

        public void ResumeDownload(EbookViewModel ebook)
        {

        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
             this.itemListView.DataContext = this.ltv_downloadingItems.DataContext = ManagerDataViewModel.GetAllDownloadingEbooks();
            
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }



        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            // not snap mode
            if (ltv_downloadingItems.Visibility == Windows.UI.Xaml.Visibility.Visible)
            {
                if (ltv_downloadingItems.SelectedItem != null)
                {
                    StopDownload((EbookViewModel)ltv_downloadingItems.SelectedItem);
                }
                else
                {
                    //do nothing
                }
            }
            else // snap mode
            {
                if (itemListView.SelectedItem != null)
                {
                    StopDownload((EbookViewModel)itemListView.SelectedItem);
                }
                else
                {
                    // do nothing
                }
            }
        }

        private void btnPause_Click(object sender, RoutedEventArgs e)
        {

            // not snap mode
            if (ltv_downloadingItems.Visibility == Windows.UI.Xaml.Visibility.Visible)
            {
                if (ltv_downloadingItems.SelectedItem != null)
                {
                    PauseDownload((EbookViewModel)ltv_downloadingItems.SelectedItem);
                }
                else
                {
                    //do nothing
                }
            }
            else // snap mode
            {
                if (itemListView.SelectedItem != null)
                {
                    PauseDownload((EbookViewModel)itemListView.SelectedItem);
                }
                else
                {
                    // do nothing
                }
            }

        }

        private void ltv_downloadingItems_SelectionChanged(object sender, Windows.UI.Xaml.Controls.SelectionChangedEventArgs e)
        {

        	// not snap mode
            if (ltv_downloadingItems.Visibility == Windows.UI.Xaml.Visibility.Visible)
            {
                if (ltv_downloadingItems.SelectedItem != null)
                {
                    bottomAppBar.IsOpen = true;
                }
                else
                {
                    bottomAppBar.IsOpen = false;
                }
            }
            else // snap mode
            {
                if (itemListView.SelectedItem != null)
                {
                    bottomAppBar.IsOpen = true;
                }
                else
                {
                    bottomAppBar.IsOpen = false;
                }
            }
        }

        private void bottomAppBar_Opened(object sender, object e)
        {
            // not snap mode
            if (ltv_downloadingItems.Visibility == Windows.UI.Xaml.Visibility.Visible)
            {
                if (ltv_downloadingItems.SelectedItem != null)
                {
                    btnCancel.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    btnPause.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
                else
                {
                    btnCancel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    btnPause.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                }
            }
            else // snap mode
            {
                if (itemListView.SelectedItem != null)
                {
                    btnCancel.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    btnPause.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
                else
                {
                    btnCancel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    btnPause.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                }
            }
        }
    }
}
